package com.lemon.cloud.core.response;


import com.lemon.cloud.core.util.MaskToStringBuilder;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * @param
 * @author lemon
 * @description http 通用返回结构
 * @return
 * @date 2019-07-27 11:20
 */
@Getter
@Setter
public class BasicResponse<T extends Serializable> {

    public static final String DEFAULT_CODE = "200";
    public static final String DEFAULT_MESSAGE = "ok";

    private String code;
    private String message;
    private String extraCode;
    private String extraMessage;
    private String tracestack;
    private String url;
    private T data;

    public BasicResponse(String code, String message, String extraCode, String extraMessage, T data) {
        this.code = StringUtils.defaultIfBlank(code, DEFAULT_CODE);
        this.message = StringUtils.defaultIfBlank(message, DEFAULT_MESSAGE);
        this.extraCode = StringUtils.trimToEmpty(extraCode);
        this.extraMessage = StringUtils.trimToEmpty(extraMessage);
        this.data = data;
    }

    public BasicResponse(String code, String message, T data) {
        this(code, message, null, null, data);
    }

    public BasicResponse(T data) {
        this(null, null, data);
    }

    public BasicResponse() {
        this(null);
    }

    @Override
    public String toString() {
        return new MaskToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
    }
}
